package com.example.springboot.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelWriter;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;
import java.net.URLEncoder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.PageParam;
import com.example.springboot.entity.PoetVo;
import com.example.springboot.service.IPoetService;
import com.example.springboot.vo.CountLocaleVO;
import com.sun.xml.internal.ws.wsdl.writer.document.Port;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot.common.Result;
import org.springframework.web.multipart.MultipartFile;
import com.example.springboot.entity.Poet;

import org.springframework.web.bind.annotation.RestController;

/**
* PoetAPI接口
*/
@RestController
@RequestMapping("/poet")
public class PoetController {

    @Autowired
    private IPoetService poetService;

    @PostMapping
    public Result save(@RequestBody Poet poet) {
        poetService.save(poet);
        return Result.success();
    }

    @PutMapping
    public Result update(@RequestBody Poet poet) {
        poetService.updateById(poet);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        poetService.removeById(id);
        return Result.success();
    }

    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        poetService.removeByIds(ids);
        return Result.success();
    }

    @GetMapping
    public Result findAll() {
        return Result.success(poetService.list());
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(poetService.getById(id));
    }

    @GetMapping("/page/type")
    public Result findPageType(@RequestParam(defaultValue = "") String name,
                               @RequestParam(defaultValue = "") String type,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Poet> queryWrapper = new QueryWrapper<Poet>().orderByDesc("id");
        queryWrapper.like(!"".equals(name), "name", name);
        queryWrapper.eq(!"".equals(type), "type", type);
        Page<Poet> page = poetService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }
    @GetMapping("/page")
    public Result findPage(@RequestParam(defaultValue = "") String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {
        QueryWrapper<Poet> queryWrapper = new QueryWrapper<Poet>().orderByDesc("id");
        queryWrapper.like(!"".equals(name), "name", name);
        Page<Poet> page = poetService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }

    @PostMapping("/getList")
    public Result getList() {
        return Result.success(poetService.getList());
    }

    @PostMapping("/getAgeList")
    public Result getAgeList() {
        List<Poet> ageList = poetService.getAgeList();
        int[][] array = ageList.stream()
                .map(e -> new int[]{e.getAge(), e.getNumber()})
                .toArray(int[][]::new);
        List<int[][]> list = new ArrayList<>();
        list.add(array);
        return Result.success(list);
    }
    @PostMapping("/list")
    public Result list() {
        return Result.success(poetService.list());
    }
    /**
     * 地点统计
     * @return
     */
    @GetMapping("/count/locale")
    public Result countLocale(PageParam param){
        CountLocaleVO countLocaleVO = poetService.countLocale(param);
        return Result.success(countLocaleVO);
    }

    @GetMapping("/count/surname")
    public Result countSurname(PageParam param){
        List<Poet> portList = poetService.countSurname(param);
        return Result.success(portList);
    }

    @PostMapping("/getMap")
    public Result getMap() {
        return Result.success(poetService.getMap());
    }

    @PostMapping("/getNameMap")
    public Result getNameMap() {
        return Result.success(poetService.getNameMap());
    }

}
